/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

/**
 *
 * @author User
 */

import engine.Singleton;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import server.ConnectClient;
import tictactoe.SmallField;

public class HttpClient {

    public static void main(String[] args) throws Throwable {
        ServerSocket ss = new ServerSocket(7777);
        while (true) {
            Socket s = ss.accept();
            System.err.println("Client accepted");
            SocketProcessor pr = new SocketProcessor(s);
            new Thread(pr).start();
            
        }
    }

    public static class SocketProcessor implements Runnable {
      
      private WebConnectClient wc;
      private String data;
      public SmallField[][] matrix = new SmallField[3][3];
      public static boolean fig;
      public static boolean step;
      public boolean[][] steps = new boolean[3][3];

        private Socket s;
        private InputStream is;
        private OutputStream os;

        public SocketProcessor(Socket s) throws Throwable {
            this.s = s;
            this.is = s.getInputStream();
            this.os = s.getOutputStream();
            Singleton.sok = this;
            //data = WebPages.connect();
            data = WebPages.mainWindow();
        }
        
        public void setWc(WebConnectClient wc)
        {
          this.wc = wc;
        }
        

        public void run() {
            try {
                readInputHeaders();
               writeResponse(data);
            } catch (Throwable t) {
            } finally {
                try {
                    s.close();
                } catch (Throwable t) {
                }
            }
            System.err.println("Client processing finished");
            
        }

        public void writeResponse(String s) throws Throwable {
            String response = "HTTP/1.1 200 OK\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + s.length() + "\r\n" +
                    "Connection: close\r\n\r\n";

            String result = response + s;
            os.write(result.getBytes());
            os.flush();
        }

        private void readInputHeaders() throws Throwable {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            int i =0;
            String res = "";
            while(true) {
                String s = br.readLine();
                if (i==0) {
                    res = s;
                }
                if(s == null || s.trim().length() == 0) {
                    break;
                }
                i++;
            }
            System.out.println(res);
            new WebClientReaction(this).getString(res);
          
        }
    }
}
